//
//  StatusRetweetedView.swift
//  Weibo
//
//  Created by 付启刚 on 15/12/21.
//  Copyright © 2015年 faq. All rights reserved.
//

import UIKit
import SnapKit
class StatusRetweetedView: UIView {
    var bottomConstraints: Constraint?
    // MARK:定义外部模型属性
    var retweetedStatus:Status?{
        didSet{
            retweetedLabel.text="@"+(retweetedStatus?.user?.name ?? "")+(retweetedStatus?.text ?? "")
            pictureView.imageURLs=retweetedStatus?.imageURLs
            //卸载底部约束
            self.bottomConstraints?.uninstall()
            //根据是否有配图 重新添加约束
            if let urls=retweetedStatus?.imageURLs where urls.count>0{
                //有配图视图
                self.snp_makeConstraints { (make) -> Void in
                    //1.添加约束
                    //2.记录底部约束
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
                }
            }else{
                //没有配图视图
                self.snp_makeConstraints { (make) -> Void in
                    //1.添加约束
                    //2.记录底部约束
                    self.bottomConstraints = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMarigin).constraint
                    
                }
            }
            
        }
    }
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor=UIColor(white: 0.98, alpha: 1)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置页面和布局
    private func setupUI(){
        addSubview(retweetedLabel)
        addSubview(pictureView)
        //添加约束
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(StatusCellMarigin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMarigin)
            make.left.equalTo(retweetedLabel.snp_left)
        }
        
        //转发微博自动计算行高
        self.snp_makeConstraints { (make) -> Void in
            //1.添加约束
            //2.记录底部约束
            self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
        }
        
    }
    //懒加载所有子视图
    private lazy var  retweetedLabel: UILabel = UILabel(title: "我是转发微博", color: UIColor.darkGrayColor(), fontSize: 14, margin: StatusCellMarigin)
    //配图视图
    private lazy var pictureView:StatusPictureView = StatusPictureView()
    
}
